package com.link.base.tencent.corpwx.externalcontact.controller;

import com.link.base.tencent.corpwx.externalcontact.model.ExternalContact;
import com.link.base.tencent.corpwx.externalcontact.service.ExternalContactService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 外部联系人 
 *
 * @author 欧阳振强
 * @date 2020-01-02 15:22:51.0
 */
@Controller
@RequestMapping("/link/corpwx/externalContact")
public class ExternalContactController extends BasicController<ExternalContact> {

    @Resource
    private ExternalContactService externalContactService;

    @Override
    public BasicService<ExternalContact> getBasicService() throws Exception {
        return externalContactService;
    }

    /**
     * 从企业微信同步客户联系人到CRM
     *
     * @author 欧阳振强
     * @date 2020/01/03
     */
    @GetMapping(value = "/syncAllAccountInfoFromWx", name = "从企业微信同步客户联系人到CRM")
    @ResponseBody
    public Map<String, Object> syncAllAccountInfo(HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            externalContactService.syncAllAccountInfoFromWx();
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 从企业微信同步单个客户联系人到CRM
     *
     * @author 欧阳振强
     * @date 2020/01/03
     */
    @GetMapping(value = "/syncSingleAccountInfoFromWx", name = "从企业微信同步单个客户联系人到CRM")
    @ResponseBody
    public Map<String, Object> syncSingleAccountInfo(HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            String externalContactId = request.getParameter("externalContactId");
            if (StringUtils.isBlank(externalContactId)) {
                throw new BasicControllerException("externalContactId不能为空！");
            }
            externalContactService.syncSingleAccountInfoFromWx(externalContactId);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}